#include "qdriveshowwidget.h"

#include "../../lib/corelib/qcommondelegate.h"
#include "../../lib/corelib/minisplitter.h"
#include "../../lib/shared/qdriveattribute.h"
#include "../../lib/shared/qabstractdrive.h"
#include "../../lib/shared/qproject.h"
#include "../../lib/shared/qdrivevaluemap.h"

#include <QVBoxLayout>
#include <QHeaderView>

QDriveShowWidget::QDriveShowWidget(const QString &name, QWidget *parent):
    QWidget(parent),
    m_attributeTree(new QCommonTreeWidget),
    m_valueTree(new QCommonTreeWidget),
    m_name(name)
{
    QVBoxLayout *l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);

    m_attributeTree->header()->setVisible(true);
    m_valueTree->header()->setVisible(true);
    m_valueTree->setRootIsDecorated(false);
    m_attributeTree->setRootIsDecorated(false);
    m_attributeTree->setHeaderLabels(QStringList()<<tr("Name")<<tr("Value"));
    m_attributeTree->resize(200,200);
    m_attributeTree->setColumnWidth(0,100);
    m_attributeTree->setColumnWidth(1,100);
    MiniSplitter *s=new MiniSplitter;
    s->addWidget(m_attributeTree);
    s->addWidget(m_valueTree);
    s->setStretchFactor(0,0);
    s->setStretchFactor(1,1);
    l->addWidget(s);

    this->setLayout(l);

    m_attributeTree->setColumnCount(2);
    QTreeWidgetItem *item;

    QAbstractDrive *drive=QProject::project()->getDrive(name).m_drive;

    if(drive!=NULL)
    {
        QList<QDriveAttribute*> attrs=drive->attributes();
        foreach(QDriveAttribute *attr,attrs)
        {
            item=new QTreeWidgetItem(m_attributeTree);
            item->setText(0,attr->name());
            item->setToolTip(0,attr->information());
            item->setText(1,attr->valueText());
            item->setToolTip(1,attr->valueText());
            item->setIcon(1,attr->valueIcon());
            item->setFlags(item->flags() | Qt::ItemIsEditable);
            item->setData(1,Qt::UserRole+0x1000,(int)attr);
            connect(attr,SIGNAL(valueChanged()),this,SLOT(attributeChanged()));
            m_attributes.insert(item,attr);
        }

        QDriveValueMap *value=drive->driveValueMap();

        m_valueTree->setColumnCount(value->columnCount()+1);

        QList<QHeaderInfo*> h=value->headers();
        m_valueTree->headerItem()->setText(0,"No.");
        for(int i=0;i<h.size();i++)
        {
            m_valueTree->headerItem()->setText(i+1,h.at(i)->property("headerText").toString());
        }
        m_valueTree->setColumnWidth(0,50);
        int column=value->columnCount();
        int row=value->rowCount();

        for(int i=0;i<row;i++)
        {
            QTreeWidgetItem *item=new QTreeWidgetItem(m_valueTree);
            item->setText(0,QString::number(i+1));
            item->setFlags(item->flags() | Qt::ItemIsEditable);
            for(int j=0;j<column;j++)
            {
                item->setText(j+1,value->value(i,j));
                item->setToolTip(j+1,value->value(i,j));
                item->setData(j+1,Qt::UserRole+0x1001,(int)value);
            }
            m_values.append(item);
        }
        connect(value,SIGNAL(valueChanged(int,int,QString)),this,SLOT(valueChanged(int,int,QString)));
    }
    connect(m_attributeTree,SIGNAL(pressed(QModelIndex)),this,SLOT(attributeClicked(QModelIndex)));
    connect(m_valueTree,SIGNAL(pressed(QModelIndex)),this,SLOT(valueClicked(QModelIndex)));


}

void QDriveShowWidget::attributeClicked(const QModelIndex &index)
{
    if(index.column()==1)
    {
        m_attributeTree->edit(index);
    }
    else
    {
        m_attributeTree->edit(index.model()->index(index.row(),1));
    }
}

void QDriveShowWidget::valueClicked(const QModelIndex &index)
{
    m_valueTree->edit(index);
}

void QDriveShowWidget::attributeChanged()
{
    QDriveAttribute *attr=(QDriveAttribute*)sender();

    QMapIterator<QTreeWidgetItem*,QDriveAttribute*> it(m_attributes);

    while(it.hasNext())
    {
        it.next();
        if(it.value()==attr)
        {
            it.key()->setText(1,attr->valueText());
            it.key()->setToolTip(1,attr->valueText());
            it.key()->setIcon(1,attr->valueIcon());
        }
    }
}

void QDriveShowWidget::valueChanged(int row, int column, const QString &value)
{
    if(row>=0 && row<m_values.size())
    {
        m_values.at(row)->setText(column+1,value);
    }
}

void QDriveShowWidget::addValue()
{
    QAbstractDrive *drive=QProject::project()->getDrive(m_name).m_drive;
    drive->driveValueMap()->addRow();
    int col=drive->driveValueMap()->columnCount();
    int row=drive->driveValueMap()->rowCount()-1;
    int vcol=drive->driveValueMap()->column("Memery");

    QTreeWidgetItem *item=new QTreeWidgetItem(m_valueTree);
    item->setText(0,QString::number(row+1));
    item->setFlags(item->flags() | Qt::ItemIsEditable);
    for(int i=0;i<col;i++)
    {
        item->setText(i+1,drive->driveValueMap()->value(row,i));
        item->setToolTip(i+1,drive->driveValueMap()->value(row,i));
        item->setData(i+1,Qt::UserRole+0x1001,(int)drive->driveValueMap());
    }
    m_values.append(item);

    if(vcol>=0)
    {
        QProject *p=QProject::project();
        for(int i=0;i<p->getRuntimeDataCount();i++)
        {
            if(p->getRuntimeData(i).isMemery())
            {
                drive->driveValueMap()->setValue(row,vcol,p->getRuntimeData(i).name());
                break;
            }
        }
    }
}

void QDriveShowWidget::removeSelectValue()
{
    QTreeWidgetItem *item;
    for(int i=0;i<m_values.size();i++)
    {
        item=m_values.at(i);
        if(item->isSelected())
        {
            QAbstractDrive *drive=QProject::project()->getDrive(m_name).m_drive;
            drive->driveValueMap()->removeRow(i);
            m_valueTree->takeTopLevelItem(i);
            m_values.removeAll(item);
            delete item;
            updateIndex();
            return;
        }
    }
}

void QDriveShowWidget::updateIndex()
{
    for(int i=0;i<m_values.size();i++)
    {
        m_values.at(i)->setText(0,QString::number(i+1));
    }
}
